package geppetto.phraseProbability.smoothing;

import geppetto.phraseTable.PhraseTable;
import geppetto.phraseTable.phrase.Phrase;

public class KneserNeyDiscount implements DiscountFunction{

	boolean initialized = false;
	double Y;

	public double getDiscount(PhraseTable phraseTable, Phrase p) {
		if(!initialized){
			initialize(phraseTable);
		}		
		return Y;
	}
	
	public void initialize(PhraseTable phraseTable){
		if(!phraseTable.countNormalized){
			phraseTable.normalizeCounts();
		}

		double n1 = phraseTable._numPhrasesWithCounts[1];
		double n2 = phraseTable._numPhrasesWithCounts[2];
		Y = n1 / (n1 + (double)2 * n2);
		System.out.println("n1 is " + n1);
		System.out.println("n2 is " + n2);
		System.out.println("the discount is " + Y);
		initialized = true;
	}
}
